package com.none.web.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.controller.BaseControllerSupport;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TUser;
import com.none.web.model.TUserApp;
import com.none.web.service.param.IParamService;
import com.none.web.service.user.IUserService;

/**
 * 用户管理Controller
 * 
 * @author Summer
 * 
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseControllerSupport {

	@Resource(name = "userService")
	IUserService userService;
	@Resource(name = "paramService")
	IParamService paramService;
	
	/**
	 * 新增用户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addUser")
	public Object addUser(@FormModel("user") TUserApp user) throws Exception {
		String[] filter = new String[] { "password", "loginTime","userType", "tuser" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("user", userService.addUser(user));
		
 		Result result = new Result(SysConstant.STATE_SUCCESS, "user.addUser", data);
//		return JSONUtil.toJSON(result, new TUser(), filter);

		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editUser")
	public Object editUser(@FormModel("user") TUser user) throws Exception {
		String[] filter = new String[] { "password", "loginTime","userType", "tuser" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("user", userService.editUser(user));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.updateSuccess", data);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delUser")
	public Object delUser(String id) throws Exception {
		userService.delUser(id);
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.delSuccess", data);
		return JSONUtil.toJSON(result);
	}

	/**
	 * 用户列表
	 * 
	 * @return List<TbUser>
	 */
	@ResponseBody
	@RequestMapping("listUser")
	public Object listUser(@FormModel("pager") Pager pager, String keyWord, String code, String status) {
		String[] filter = new String[] { "password", "loginTime","userType" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("users", userService.listUser(pager, keyWord, code, status));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.listUser", data);
		return JSONUtil.toJSON(result,filter);
		// return JSONUtil.toJSON(result);
	}

	/**
	 * app端用户激活
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @param code
	 *            激活码
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("activeUser")
	public Object activeUser(String userName, String password, String code, String udid) throws Exception {
		String[] filter = new String[] { "password", "loginTime","userType", "tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.mergeActiveUser(userName, password, code, udid));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.activeUser", userMap);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * app用户登录
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("loginUser")
	public Object loginUser(String userName, String password, String udid) throws Exception {
		//
		String[] filter = new String[] {"tactivityAppUsers" ,"password", "loginTime","userType", "tcodes" ,"TBeacons"};
		Map<String, Object> userMap = new HashMap<String, Object>();
		TUserApp tUser = userService.mergeLoginUser(userName, password, udid);
		userMap.put("user", tUser);
 		Result result = new Result(SysConstant.STATE_SUCCESS, "user.loginUser", userMap);
		if (tUser == null) {
			result = new Result(SysConstant.STATE_ERROR, "user.loginError", null);
		}
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 重置用户
	 * 
	 * @param userName
	 *            用户名
	 * 
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("resetUser")
	public Object resetUser(String userName) throws Exception {
		String[] filter = new String[] { "password", "loginTime","userType", "tuser" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.mergeResetUser(userName));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.resetUser", userMap);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 修改用户密码
	 * 
	 * @param userName
	 *            用户名
	 * @param oldPassword
	 *            旧密码
	 * @param password
	 *            新密码
	 * @return 用户信息
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("resetUserPassword")
	public Object resetUserPassword(String userName, String oldPassword, String password) throws Exception {
		String[] filter = new String[] {"tactivityAppUsers", "password", "loginTime","userType", "tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.resetUserPassword(userName, oldPassword, password));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.resetUserPassword", userMap);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 统计所有的app用户的信息
	 * 
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("countAllUser")
	public Object countAllUser() throws Exception {
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.countAllUser());
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.countAllUser", userMap);
		return JSONUtil.toJSON(result);
	}

	/**
	 * 得到用户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getConsoleUser")
	public Object getConsoleUser(String userId) throws Exception {
		String[] filter = new String[] { "password", "lastTime", "tcodes" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("user", userService.getConsoleUser(userId));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.getUserInfo", data);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 新增权限用户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addConsoleUser")
	public Object addConsoleUser(@FormModel("user") TUser user) throws Exception {
		String[] filter = new String[] { "password", "lastTime", "tcodes" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("user", userService.addConsoleUser(user));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.addUser", data);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 修改权限用户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editConsoleUser")
	public Object editConsoleUser(@FormModel("user") TUser user) throws Exception {
		String[] filter = new String[] { "password", "lastTime", "tcodes" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("user", userService.editConsoleUser(user));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.editUser", data);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 用户列表
	 * 
	 * @return List<TbUser>
	 */
	@ResponseBody
	@RequestMapping("listConsoleUser")
	public Object listConsoleUser(@FormModel("pager") Pager pager, String keyWord) {
		String[] filter = new String[] { "password", "lastTime", "tcodes" };

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("users", userService.listConsoleUser(pager, keyWord));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.listUser", data);
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * 权限用户登录
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * 
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("loginConsoleUser")
	public Object loginConsoleUser(String userName, String password,HttpSession session) throws Exception {
		String[] filter = new String[] {"password", "lastTime","tcodes" };

		Map<String, Object> userMap = new HashMap<String, Object>();

		TUser tUser = userService.mergeLoginConsoleUser(userName, password,session);
		userMap.put("user", tUser);
		userMap.put("param", paramService.getParamByKey("fileSize"));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.loginUser", userMap);
		if (tUser == null) {
			result = new Result(SysConstant.STATE_ERROR, "user.loginConsoleError", null);
		}

		/*SessionControl*/
		session.setAttribute("AuthenticatedUserId", tUser.getUserId());
		/*SessionControl*/
		
		return JSONUtil.toJSON(result,filter);
	}

	/**
	 * logoff console user
	 * 
	 * @param session
	 * 
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("logoffConsoleUser")
	public Object logoffConsoleUser(HttpSession session) throws Exception {

		Result result = new Result(SysConstant.STATE_SUCCESS, "user.logoffUser", null);
		
		session.setAttribute("AuthenticatedUserId", null);
		
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * 创建新的用户的密码
	 * 
	 * 
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getNewUserPassword")
	public Object getNewUserPassword() throws Exception {
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.getNewUserPassword());
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.createNewPassword", userMap);
		return JSONUtil.toJSON(result);
	}

	/**
	 * 修改用户状态
	 * 
	 * 
	 * @return Object
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editUserState")
	public Object editUserState(String userId, int state) throws Exception {
		String[] filter = new String[] {"tactivityAppUsers", "password", "lastTime","tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.editUserState(userId, state));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.editUserState", userMap);
		return JSONUtil.toJSON(result,filter);
	}
	
	/**
	 * APP用户激活
	 * 需要前端传过来用户信息及扫描二维码获得数据
	 * 将传过来的二维码与staffId加密后进行对比，如果结果一致，则添加该用户到数据库，反之不能添加该用户
	 * @return Object
	 * @throws Exception
	 * @author Bing  2014-11-14 19:49
	 */
	@ResponseBody
	@RequestMapping("activeUserByCompare")
	public Object activeUserByCompareCode(@FormModel("user") final TUserApp userApp,String staffId,String code,String udid) throws Exception {
		String[] filter = new String[] { "tactivityAppUsers","password", "lastTime","tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.activeUserByCompareCode(userApp, staffId,code,udid));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.activeUser", userMap);
		return JSONUtil.toJSON(result,filter);
	}
	
	/**
	 * 激活二维码
	 * @author Bing  2014-11-21
	 */
	@ResponseBody
	@RequestMapping("activeQRCode")
	public Object activeQRCode(String code,String udid) throws Exception {
		String[] filter = new String[] {"tactivityAppUsers", "password", "lastTime","tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("userInfo", userService.activeQRCode(code,udid));
		
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.codeRigth", userMap);
		return JSONUtil.toJSON(result,filter);
	}
	
	
	/**
	 * 修改用户密码
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            新密码
	 * @return 用户信息
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("activeSetPassword")
	public Object activeSetPassword(String staffId,String userName, String password,String udid) throws Exception {
		//william modify 2014-12-07. add filter "tactivityAppUsers". 
		String[] filter = new String[] { "tactivityAppUsers","password", "loginTime","userType", "tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("userInfo", userService.activeSetPassword(staffId,userName, password,udid));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.resetUserPassword", userMap);
		return JSONUtil.toJSON(result,filter);
	}
	
	/**
	 * forget password QR code
	 * @author William  2014-12-8
	 */
	@ResponseBody
	@RequestMapping("forgetPasswordQRCode")
	public Object forgetPasswordQRCode(String code) throws Exception {
		String[] filter = new String[] { "tactivityAppUsers","password", "loginTime","userType", "tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("userInfo", userService.forgetPasswordQRCode(code));
		
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.codeRigth", userMap);
		return JSONUtil.toJSON(result,filter);
	}
	
	/**
	 * forget set password
	 * @author William  2014-12-8
	 */
	@ResponseBody
	@RequestMapping("forgetSetPassword")
	public Object forgetSetPassword(String staffId, String password) throws Exception {
		String[] filter = new String[] {"tactivityAppUsers", "password", "loginTime","userType", "tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("user", userService.activeForgetSetPassword(staffId, password));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.resetUserPassword", userMap);
		return JSONUtil.toJSON(result,filter);
	}
	/**
	 * change device QR code
	 * @author William  2014-12-8
	 */
	@ResponseBody
	@RequestMapping("changeDeviceQRCode")
	public Object changeDeviceQRCode(String code,String udid) throws Exception {
		String[] filter = new String[] { "tactivityAppUsers","password", "loginTime","userType", "tcodes" };
		Map<String, Object> userMap = new HashMap<String, Object>();
		userMap.put("userInfo", userService.activeChangeDeviceQRCode(code,udid));
		
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.updateSuccess", userMap);
		return JSONUtil.toJSON(result,filter);
	}
	
	
//	/**原生直接调用
//	 * 设置用户的push_id， uuid 和  设备类型
//	 */
//	@ResponseBody
//	@RequestMapping("setPushInfo")
//	public Object setPushInfo(String udid,String push_id,String device_type) throws Exception {
//		Map<String, Object> returnmap = new HashMap<String, Object>();
//		returnmap.put("state", userService.setPushInfo(udid, push_id, device_type));
//		Result result = new Result(SysConstant.STATE_SUCCESS, "user.updateSuccess", returnmap);
//		return JSONUtil.toJSON(result);
//	}
	
	
	/**原生直接调用
	 * 设置用户推送相关信息
	 */
	@ResponseBody
	@RequestMapping("setPushInfo")
	public Object setPushInfo(String udid,String channelId,String baiduUserId,String device_type) throws Exception {
		Map<String, Object> returnmap = new HashMap<String, Object>();
		returnmap.put("state", userService.setPushInfo(udid, channelId,baiduUserId, device_type));
		Result result = new Result(SysConstant.STATE_SUCCESS, "user.updateSuccess", returnmap);
		return JSONUtil.toJSON(result);
	}
}
